package com.example.demo.service;

import com.example.demo.mapper.CommentMapper;
import com.example.demo.model.Comment;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CommentService {

    private final CommentMapper commentMapper;

    // 构造函数注入 CommentMapper，实现依赖注入
    public CommentService(CommentMapper commentMapper) {
        this.commentMapper = commentMapper;
    }

    // 获取所有论坛帖子
    public List<Comment> getAllComments() {
        return commentMapper.selectAllComments();
    }

    // 根据ID查询论坛帖子
    public Comment getCommentById(Long id) {
        return commentMapper.selectCommentById(id);
    }

    // 根据用户名查询论坛帖子
    public List<Comment> getCommentByUsername(String username) {
        return commentMapper.selectCommentByUserName(username);
    }

    // 新增论坛帖子
    public void createComment(Comment comment) {
        // 设置创建时间和回复时间为当前时间
        LocalDateTime now = LocalDateTime.now();
        comment.setCreateTime(now);
        comment.setReplyTime(now);
        commentMapper.insertComment(comment);
    }

    // 删除论坛帖子
    public void deleteComment(Long id) {
        commentMapper.deleteCommentById(id);
    }

    // 更新论坛帖子内容和回复时间
    public void updateComment(Comment comment) {
        comment.setReplyTime(LocalDateTime.now());
        commentMapper.updateComment(comment);
    }
}